package net.celisdelafuente.java.Acacia.entity;

import java.sql.SQLException;
import java.util.ArrayList;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName="book_classifications")
public class Classification {
	
	public static final String ID_FIELD_NAME = "id";
	public static final String CKEY_FIELD_NAME = "ckey";
	public static final String DESC_FIELD_NAME = "description";
	
	@DatabaseField(columnName=ID_FIELD_NAME, generatedId=true)
	private int id;
	
	@DatabaseField(columnName=CKEY_FIELD_NAME, canBeNull=false)
	private String ckey;
	
	@DatabaseField(columnName=DESC_FIELD_NAME, canBeNull=false)
	private String description;

	public Classification() {
		// TODO Auto-generated constructor stub
	}
	
	public Classification(String ckey, String description) {
		this.ckey = ckey;
		this.description = description;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCkey() {
		return ckey;
	}

	public void setCkey(String ckey) {
		this.ckey = ckey;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	@SuppressWarnings("deprecation")
	public Classification getByClassification(ConnectionSource conn,
			Dao<Classification, Integer> dao, String ckey, String description) {
		Classification c = this;
		if(!description.isEmpty()) {
			ArrayList<Classification> classification = new ArrayList<Classification>();
			QueryBuilder<Classification, Integer> qb = dao.queryBuilder();
			try {
				
				qb.where().and(qb.where().eq(Classification.DESC_FIELD_NAME, 
						description),
						qb.where().eq(Classification.CKEY_FIELD_NAME, ckey));
				qb.limit(1);
				PreparedQuery<Classification> pq = qb.prepare();
				classification = (ArrayList<Classification>)dao.query(pq);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(!classification.isEmpty())
				c=classification.get(0);
		}		
		return c;
	}

}
